#include "apue.h"
#define OPEN_MAX_GUESS 256

int my_dup(int fd1, int fd2) {
	int fd[OPEN_MAX_GUESS];
	if (fd1 < 0 || fd1 > OPEN_MAX_GUESS) {
		printf("fd1 error\n");
		return -1;
	}
	if (fd2 < 0 || fd2 > OPEN_MAX_GUESS || fd2 == fd1) {
		printf("fd2 error\n");
		return -1;
	}
	int fd3 = -1, foot = 0;
	while (fd3 != fd2) {
		fd3 = dup(fd1);
		fd[foot++] = fd3;
	}
	for (int i = 0; i < foot - 1; i++) {
		close(fd[i]);
	}
	printf("foot = %d\n", foot);
	return fd2;
}

int main() {
	int fd = open("test", O_RDWR | O_APPEND);
	dup2(fd, 95);
	int fd2 = my_dup(fd, 100);
	printf("fd = %d, fd2 = %d\n", fd, fd2);
	err_sys()
	return 0;
}
